<html>
<body onload="setupHandlers()">

<h3>HTML mimetype</h3>

<a download id="download-link">&lt;a download link&gt;</a>
<br><br>
<button id='navigate-top-frame-to-html'>
  Navigate top frame to filesystem URL HTML
</button>
<br>
<button id='window-open-html'>
  Open new window with a filesystem URL HTML
</button>
<button id='window-open-redirect'>
  Open new window with a redirect to filesystem URL
</button>
<br>
<form method="post" id="form-post-to-html-form">
  <input type=submit id='form-post-to-html'
      value="Submit form to filesystem URL HTML">
</form>

<h3>octet-stream mimetype</h3>

<button id='navigate-top-frame-to-octetstream'>
  Navigate top frame to filesystem URL octet-stream
</button>
<br>
<button id='window-open-octetstream'>
  Open new window with a filesystem URL octet-stream
</button>
<form method="post" id="form-post-to-octetstream-form">
  <input type=submit id="form-post-to-octetstream"
      value="Submit form to filesystem URL octet-stream">
</form>
<h3>PDF mimetype</h3>

<button id='navigate-top-frame-to-pdf'>
  Navigate top frame to filesystem URL PDF
</button>
<br>
<button id='window-open-pdf'>
  Open new window with a filesystem URL PDF
</button>
<br>
<form method="post" id="form-post-to-pdf-form">
  <input type=submit id='form-post-to-pdf'
      value="Submit form to filesystem URL PDF">
</form>

<h3>Unknown mimetype</h3>

<button id='navigate-top-frame-to-unknown-mimetype'>
  Navigate top frame to filesystem URL unknown mimetype
</button>
<br>
<button id="window-open-unknown-mimetype">
  Open new window with a filesystem URL unknown mimetype
</button>
<form method="post" id="form-post-to-unknown-mimetype-form">
  <input type=submit id='form-post-to-unknown-mimetype'
         value='Submit form to filesystem URL unknown mimetype'>
</form>

<script>
var PDF =
  "%PDF-1.7\n" +
  "1 0 obj << /Type /Page /Parent 3 0 R /Resources 5 0 R /Contents 2 0 R >>\n" +
  "endobj\n" +
  "2 0 obj << /Length 51 >>\n" +
  " stream BT\n" +
  " /F1 12 Tf\n" +
  " 1 0 0 1 100 20 Tm\n" +
  " (Hello World)Tj\n" +
  " ET\n" +
  " endstream\n" +
  "endobj\n" +
  "3 0 obj << /Type /Pages /Kids [ 1 0 R ] /Count 1 /MediaBox [ 0 0 300 50] >>\n" +
  "endobj\n" +
  "4 0 obj << /Type /Font /Subtype /Type1 /Name /F1 /BaseFont/Arial >>\n" +
  "endobj\n" +
  "5 0 obj << /ProcSet[/PDF/Text] /Font <</F1 4 0 R >> >>\n" +
  "endobj\n" +
  "6 0 obj << /Type /Catalog /Pages 3 0 R >>\n" +
  "endobj\n" +
  "trailer << /Root 6 0 R >>";

function setupHandlers() {
  window.webkitRequestFileSystem(window.TEMPORARY, 1024, fs => {
    // HTML mime type:
    fs.root.getFile('test.html', {create: true}, entry => {
      entry.createWriter(w => {
        w.write(new Blob([
            "NAVIGATION_SUCCESSFUL" +
            "<script" + ">console.log('NAVIGATION_SUCCESSFUL')<" + "/script>"],
            {type: "text/html"}));
      });
      document.getElementById("download-link").href = entry.toURL();
      document.getElementById("navigate-top-frame-to-html").onclick = function() {
        top.location.href = entry.toURL();
      };
      document.getElementById("window-open-html").onclick = function() {
        window.open(entry.toURL());
      };
      document.getElementById("window-open-redirect").onclick = function() {
        window.open("/server-redirect?" + entry.toURL());
      };
      document.getElementById("form-post-to-html-form").action = entry.toURL();

      // PDF:
      fs.root.getFile('test.pdf', {create: true}, entry => {
        entry.createWriter(w => {
          var blob = new Blob([PDF], {type: "application/pdf"});
          w.write(blob);
        });
        document.getElementById("navigate-top-frame-to-pdf").onclick = function() {
          top.location.href = entry.toURL();
        };
        document.getElementById("window-open-pdf").onclick = function() {
          window.open(entry.toURL());
        };
        document.getElementById("form-post-to-pdf-form").action = entry.toURL();

        // Binary mime type:
        fs.root.getFile('test.binary', {create: true}, entry => {
          entry.createWriter(w => {
            w.write(new Blob(["testdata"], {type: "application/octet-stream"}));
          });
          document.getElementById("navigate-top-frame-to-octetstream").onclick = function() {
            top.location.href = entry.toURL();
          };
          document.getElementById("window-open-octetstream").onclick = function() {
            window.open(entry.toURL());
          };
          document.getElementById("form-post-to-octetstream-form").action = entry.toURL();

          // Unknown mime type:
          fs.root.getFile('test.unknown', {create: true}, entry => {
            entry.createWriter(w => {
              w.write(new Blob(["test"], {type: "unknown/mimetype"}));
            });
            document.getElementById("navigate-top-frame-to-unknown-mimetype").onclick = function() {
              top.location.href = entry.toURL();
            };
            document.getElementById("window-open-unknown-mimetype").onclick = function() {
              window.open(entry.toURL());
            };
            document.getElementById("form-post-to-unknown-mimetype-form").action = entry.toURL();

            if (window.domAutomationController)
              window.domAutomationController.send("READY");
          });
        });
      });
    });
  });
}
</script>
</body>
</html>
